package com.cep.client;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.TextView;

public class CEPAndroidClientActivity extends Activity {
	private TextView ownIPTV = null;
	private EditText serverIPTV = null;
	private EditText serverPortTV = null;
	private EditText nameTV = null;
	private Spinner roomsSpinner = null;
	private RatingBar ratingBar = null;
	private Button button = null;
	private TextView consoleOutputTV = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		consoleOutputTV = (TextView) findViewById(R.id.textView6);
		serverIPTV = (EditText) findViewById(R.id.editText1);
		ownIPTV = (TextView) findViewById(R.id.textView2);
		serverPortTV = (EditText) findViewById(R.id.editText2);
		nameTV = (EditText) findViewById(R.id.editTextName);

		UDPSender.init(consoleOutputTV);

		ownIPTV.setText(UDPSender.instance().getLocalIpAddress());

		ServerIPWatcher.init(consoleOutputTV, this);
		serverIPTV.setText(ServerIPWatcher.instance().getServerIP());
		serverIPTV.addTextChangedListener(ServerIPWatcher.instance());

		ServerPortWatcher.init(consoleOutputTV, this);
		serverPortTV.setText(ServerPortWatcher.instance().getServerPort());
		serverPortTV.addTextChangedListener(ServerPortWatcher.instance());

		NameWatcher.init(consoleOutputTV, this);
		nameTV.setText(NameWatcher.instance().getName());
		nameTV.addTextChangedListener(NameWatcher.instance());

		RoomListener.init(consoleOutputTV);
		roomsSpinner = (Spinner) findViewById(R.id.spinner1);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter
				.createFromResource(this, R.array.rooms_array,
						android.R.layout.simple_spinner_item);
		adapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		roomsSpinner.setAdapter(adapter);
		roomsSpinner.setOnItemSelectedListener(RoomListener.instance());

		ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
		ratingBar.setNumStars(5);
		ratingBar.setStepSize((float) 0.5);
		ButtonListener.init(consoleOutputTV, ratingBar);
		button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(ButtonListener.instance());
	}

	public void enableInput(boolean flag) {
		roomsSpinner.setEnabled(flag);
		button.setEnabled(flag);
		ratingBar.setEnabled(flag);
	}
}